package com.skynet.vpn.free.view

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import com.example.myapplication.R
import com.example.myapplication.px

class ConnectView @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    var connectingColor = Color.parseColor("#248ED4")
    private val fontMetrics = Paint.FontMetrics()
    private val bounds = Rect()


    init {
        setBackgroundColor(Color.parseColor("#2A2E33"))

    }

    val animation = ObjectAnimator.ofFloat(-90f, 630f)

    var isConnecting = false
    var isDrawCenterText = false
    var centerText = ""

    var isDrawCenterIcon = true
    var isConnected = false
    var isDrawBg = false
    var centerIcon =
        if (isConnected) {
            (resources.getDrawable(R.mipmap.connected_icon) as BitmapDrawable).bitmap
        } else {
            (resources.getDrawable(R.mipmap.idel_icon) as BitmapDrawable).bitmap
        }

    var bitmap = if (isConnecting) {
        (resources.getDrawable(R.mipmap.connect_ing_bg) as BitmapDrawable).bitmap
    } else {
        (resources.getDrawable(R.mipmap.connecting_bg) as BitmapDrawable).bitmap
    }


    var isDrawConnectIngArc = false
    var size = bitmap.height.toFloat()

    val paint = Paint()
    val bigCircleWidth = 12f.px
    var startAngle = -90f
    var endAngle = startAngle + 270f

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setMeasuredDimension(size.toInt(), size.toInt())
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onDraw(canvas: Canvas) {

        centerText = if (isConnecting) "Connecting…" else "Disconnecting…"


        if (isDrawBg) {
            canvas.drawBitmap(bitmap, 0f, 0f, null)
        }
        paint.style = Paint.Style.STROKE
        paint.isAntiAlias = true
        paint.color = Color.parseColor("#212429")
        paint.strokeWidth = bigCircleWidth
        canvas.drawCircle(
            (size) / 2,
            (size) / 2,
            110f.px,
            paint
        )




        paint.color = Color.parseColor("#17171B")
        paint.style = Paint.Style.FILL_AND_STROKE

        canvas.drawCircle(
            (size) / 2,
            (size) / 2,
            80f.px,
            paint
        )


        if (isDrawCenterText) {

            paint.color = Color.WHITE
            paint.textSize = 16f.px
            paint.style = Paint.Style.FILL
            paint.getFontMetrics(fontMetrics)
            paint.getTextBounds(centerText, 0, centerText.length, bounds)

            canvas.drawText(
                centerText,
                size / 2 - bounds.width() / 2,
                height / 2f - (fontMetrics.ascent + fontMetrics.descent) / 2f,
                paint
            )
        }

        if (isDrawCenterIcon) {
            canvas.drawBitmap(
                centerIcon,
                size / 2 - centerIcon.width / 2,
                size / 2 - centerIcon.height / 2,
                null
            )
        }



        paint.color = connectingColor
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 220f.px / 2 - 160f.px / 2f - bigCircleWidth
        paint.strokeCap = Paint.Cap.ROUND

        if (isDrawConnectIngArc) {
            canvas.drawArc(
                (size - 214f.px) / 2 + bigCircleWidth,
                (size - 214f.px) / 2 + bigCircleWidth,
                size - ((size - 214f.px) / 2 + bigCircleWidth),
                size - ((size - 214f.px) / 2 + bigCircleWidth),
                startAngle,
                endAngle,
                false,
                paint
            )
        }

        if (isConnected) {
            paint.style = Paint.Style.STROKE
            paint.color = Color.parseColor("#47E580")
            paint.style = Paint.Style.STROKE
            paint.strokeWidth = 220f.px / 2 - 160f.px / 2f - bigCircleWidth
            canvas.drawArc(
                (size - 214f.px) / 2 + bigCircleWidth,
                (size - 214f.px) / 2 + bigCircleWidth,
                size - ((size - 214f.px) / 2 + bigCircleWidth),
                size - ((size - 214f.px) / 2 + bigCircleWidth),
                0f,
                360f,
                false,
                paint
            )
        }


    }

    fun startConnecting() {

        animation.duration = 3000

        animation.repeatCount = -1
        animation.interpolator = LinearInterpolator()
        isConnected = false
        isConnecting = true
        isDrawCenterIcon = false
        isDrawConnectIngArc = true
        isDrawCenterText = true
        isDrawBg = true
        centerIcon =
            if (isConnected) {
                (resources.getDrawable(R.mipmap.connected_icon) as BitmapDrawable).bitmap
            } else {
                (context.resources.getDrawable(R.mipmap.idel_icon) as BitmapDrawable).bitmap
            }

        bitmap = if (isConnected) {
            (resources.getDrawable(R.mipmap.connecting_bg) as BitmapDrawable).bitmap
        } else {
            (resources.getDrawable(R.mipmap.connect_ing_bg) as BitmapDrawable).bitmap
        }
        animation.addUpdateListener(upDataListener)
        animation.start()
    }

    private val upDataListener = ValueAnimator.AnimatorUpdateListener {
        startAngle = (it.animatedValue as Float)

        endAngle = 180f

        Log.e("TAG", "startAngle ==   $startAngle   swipeAngle==$endAngle")
        invalidate()
    }

    fun setConnected() {
        animation.cancel()
        isConnecting = false
        isConnected = true
        isDrawBg = true
        isDrawCenterText = false
        isDrawCenterIcon = true
        isDrawConnectIngArc = false
        centerIcon =
            if (isConnected) {
                (resources.getDrawable(R.mipmap.connected_icon) as BitmapDrawable).bitmap
            } else {
                (context.resources.getDrawable(R.mipmap.idel_icon) as BitmapDrawable).bitmap
            }

        bitmap = if (isConnected) {
            (resources.getDrawable(R.mipmap.connecting_bg) as BitmapDrawable).bitmap
        } else {
            (resources.getDrawable(R.mipmap.connect_ing_bg) as BitmapDrawable).bitmap
        }
        invalidate()
    }

    fun setDisConnecting() {
        isConnecting = false
        isConnected = false
        isDrawBg = true
        isDrawCenterText = true
        isDrawCenterIcon = false
        isConnected = false
        isDrawConnectIngArc = true
        centerIcon =
            if (isConnected) {
                (resources.getDrawable(R.mipmap.connected_icon) as BitmapDrawable).bitmap
            } else {
                (context.resources.getDrawable(R.mipmap.idel_icon) as BitmapDrawable).bitmap
            }

        bitmap = if (isConnected) {
            (resources.getDrawable(R.mipmap.connecting_bg) as BitmapDrawable).bitmap
        } else {
            (resources.getDrawable(R.mipmap.connect_ing_bg) as BitmapDrawable).bitmap
        }
        animation.start()
    }

    fun reset() {
        isConnecting = false
        isDrawCenterText = false

        isDrawCenterIcon = true
        isConnected = false
        isDrawBg = false
        isDrawConnectIngArc = false
        animation.cancel()
        centerIcon =
            if (isConnected) {
                (resources.getDrawable(R.mipmap.connected_icon) as BitmapDrawable).bitmap
            } else {
                (context.resources.getDrawable(R.mipmap.idel_icon) as BitmapDrawable).bitmap
            }

        bitmap = if (isConnected) {
            (resources.getDrawable(R.mipmap.connecting_bg) as BitmapDrawable).bitmap
        } else {
            (resources.getDrawable(R.mipmap.connect_ing_bg) as BitmapDrawable).bitmap
        }

        invalidate()

    }


}